//    https://www.nowcoder.com/practice/2c6a0a8e1d20492f92941400036e0890


#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string s1, s2, ss;
    cin >> s1 >> s2;
    int l = -1, r = -1;
    int m = 0x3f3f3f3f;

    for (int i = 0; i < n; i++)
    {
        cin >> ss;
        if (ss == s1)
        {
            l = i;
            if (r != -1)
                m = min(m, abs(r - l));
        }
        else if (ss == s2)
        {
            r = i;
            if (l != -1)
                m = min(m, abs(r - l));
        }
    }
    if (l == -1 || r == -1)
        cout << -1 << endl;
    else
        cout << m;

    return 0;
}
